/******************************************************************************
 * Copyright 2022 The Untouch Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/
#include "modules/calibration/lidar_imu_calibrator/include/save_extrinsic_parameters_dialog.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QStringList>

#include "ui_save_extrinsic_parameters_dialog.h"

SaveExtrinsicParametersDialog::SaveExtrinsicParametersDialog(
    const QVector<int>& extrinsic_paramter_Type_indexs, QWidget* parent)
    : QDialog(parent), ui(new Ui::SaveExtrinsicParametersDialog) {
  ui->setupUi(this);
  this->setFixedSize(this->size());
  extrinsic_types_ << "First Lidar IMU Extrinsic Parameter";
  extrinsic_types_ << "Second Lidar IMU Extrinsic Parameter";
  if (extrinsic_paramter_Type_indexs.count() > 0 &&
      extrinsic_paramter_Type_indexs.count() <= 2) {
    for (auto& index : extrinsic_paramter_Type_indexs) {
      ui->cbExtrinsicType->addItem(extrinsic_types_[index], index);
    }
  }
}

SaveExtrinsicParametersDialog::~SaveExtrinsicParametersDialog() { delete ui; }

QString SaveExtrinsicParametersDialog::GetSavePath() { return save_path_; }

int SaveExtrinsicParametersDialog::GetExtrinsicParameterTypeIndex() {
  return extrinsic_paramter_Type_index_;
}

void SaveExtrinsicParametersDialog::on_btnSavePathBrowser_clicked() {
  QString file_name = QFileDialog::getSaveFileName(
      nullptr, "Save Extrinic File", ".", QString("yaml (*.yaml)"));
  if (!file_name.isEmpty()) {
    this->ui->txtSavePath->setText(file_name);
  }
}

void SaveExtrinsicParametersDialog::on_btnConfirm_clicked() {
    if (IsInputValid()) {
      accept();
    }
}

void SaveExtrinsicParametersDialog::on_btnCancel_clicked() { close(); }

bool SaveExtrinsicParametersDialog::IsInputValid() {
  if (this->ui->cbExtrinsicType->currentText().isEmpty()) {
    QMessageBox::warning(
        this, QStringLiteral("Prompt"),
        QStringLiteral("Extrinsic Parameter Type is empty, please select!"));
    return false;
  }
  extrinsic_paramter_Type_index_ =
      this->ui->cbExtrinsicType->currentData().toInt();

  save_path_ = this->ui->txtSavePath->text();
  if (save_path_.isEmpty()) {
    QMessageBox::warning(this, QStringLiteral("Prompt"),
                         QStringLiteral("Save Path is empty, please enter!"));
    return false;
  }
  return true;
}
